//
//  ViewController.m
//  WXShareDemo
//
//  Created by Jerry on 2019/5/13.
//  Copyright © 2019 Jerry. All rights reserved.
//

#import "ViewController.h"
#import "WeChatSDK1.8.4/WXApi.h"
#import "WechatAuthSDK.h"
#import "WXApiRequestHandler.h"
#import "Constant.h"
#import "WXApiManager.h"

@interface ViewController ()<WXApiManagerDelegate>
@property (nonatomic) enum WXScene currentScene;

@end

@implementation ViewController
@synthesize currentScene = _currentScene;

- (void)viewDidLoad {
    [super viewDidLoad];

    [WXApi registerApp:@"wxb0959931d6abb45b" enableMTA:YES];
    [WXApiManager sharedManager].delegate = self;
    // Do any additional setup after loading the view.
}

#pragma mark - Action
//好友-图片分享
- (IBAction)sessionSceneBtnAction:(id)sender {
    _currentScene = WXSceneSession;

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test1m" ofType:@"png"];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    UIImage *thumbImage = [UIImage imageNamed:@"amway.jpg"];
    
    [self sendImageContentWithData:imageData andThumbImage:thumbImage WXScene:0];
    
}

//朋友圈-链接分享
- (IBAction)timelineSceneLinkAction:(id)sender {
   
    _currentScene = WXSceneTimeline;
    UIImage *thumbImage = [UIImage imageNamed:@"amway.jpg"];
    NSString *tUrl = @"https://ch.amwaynet.com.cn/content/china/accl/ch/home-care/2016/0923005.html";
    [self sendLinkContentWithURL:tUrl kTitle:kLinkTitle kDescription:kLinkDescription thumbImage:thumbImage WXScene:1];

    
}

//好友-链接分享
- (IBAction)friendSceneLinkAction:(id)sender {
    _currentScene = WXSceneSession;
    UIImage *thumbImage = [UIImage imageNamed:@"amway.jpg"];
    NSString *tUrl = @"https://ch.amwaynet.com.cn/content/china/accl/ch/home-care/2016/0923005.html";
    
    [self sendLinkContentWithURL:tUrl kTitle:kLinkTitle kDescription:kLinkDescription thumbImage:thumbImage WXScene:0];

}

//朋友圈-图片分享
- (IBAction)timelineSceneBtnAction:(id)sender {
    _currentScene = WXSceneTimeline;
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"amway" ofType:@"jpg"];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    UIImage *thumbImage = [UIImage imageNamed:@"amway.jpg"];
    [self sendImageContentWithData:imageData andThumbImage:thumbImage WXScene:1];
    
}


- (void)sendImageContent {

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"amway" ofType:@"jpg"];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];

    UIImage *thumbImage = [UIImage imageNamed:@"amway.jpg"];
    [WXApiRequestHandler sendImageData:imageData
                               TagName:kImageTagName
                            MessageExt:kMessageExt
                                Action:kMessageAction
                            ThumbImage:thumbImage
                               InScene:_currentScene];
}


- (void)sendImageContentWithData:(NSData *)imageData andThumbImage:(UIImage *)thumbImage WXScene:(int)flag{
    
//    ViewController *vc = [[ViewController alloc] init];
    
    if (flag == 1) {
        _currentScene = WXSceneTimeline;
    }else{
        _currentScene = WXSceneSession;
    }
    
    BOOL isLarge = [self thumbDataLargerThan10MB:imageData];
    if (isLarge) {
        [self showAlertWithTitle:@"Invalid image size!" andMessage:@"Share image no more than 10MB"];
    }else{
        [WXApiRequestHandler sendImageData:imageData
                                   TagName:kImageTagName
                                MessageExt:kMessageExt
                                    Action:kMessageAction
                                ThumbImage:thumbImage
                                   InScene:_currentScene];
    }
}


- (void)sendLinkContentWithURL:(NSString *)url kTitle:(NSString *)title kDescription:(NSString *) kDescription thumbImage:(UIImage *)thumbImage WXScene:(int)flag{
    
    
    if (flag == 1) {
        _currentScene = WXSceneTimeline;
    }else{
        _currentScene = WXSceneSession;
    }
    
    [WXApiRequestHandler sendLinkURL:url
                             TagName:kLinkTagName
                               Title:title
                         Description:kDescription
                          ThumbImage:thumbImage
                             InScene:_currentScene];
    
}


#pragma mark - WXApiManagerDelegate

- (void)managerDidRecvMessageResponse:(SendMessageToWXResp *)response {
    
    NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", response.errCode];
    NSLog(@"strMsg====%@",strMsg);
    if (response.errCode == 0) {
        NSLog(@"share success !");
    }else{
       
    }
}


- (BOOL)thumbDataLargerThan10MB:(NSData *)data{
    
    float res = data.length/1024.00f/1024.00f;
    
    NSLog(@"res === %f",res);
    if(res > 10){
        return YES;
    }else{
        return NO;
        
    }
}

- (void)showAlertWithTitle:(NSString *)titleStr andMessage:(NSString *)msgStr{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:titleStr message:msgStr preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"click confirm");
    }];
//    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//        NSLog(@"click cancel");
//    }];
    
    [alert addAction:confirm];
//    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}


@end
